﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AC.AvalonControlsLibrary.Controls;

namespace ImageTagEditor
{
	/// <summary>
	/// Interaction logic for NullableDateTimePicker.xaml
	/// </summary>
	public partial class NullableDateTimePicker : UserControl
	{


		/// <summary>
		/// Create a new NullableDateTimePicker
		/// </summary>
		public NullableDateTimePicker()
		{
			InitializeComponent();
		}

		#region DateTime property

		/// <summary>
		/// Get or set the DateTime Property
		/// </summary>
		public DateTime? DateTime
		{
			get { return (DateTime?)GetValue(DateTimeProperty); }
			set { SetValue(DateTimeProperty, value); }
		}

		// Using a DependencyProperty as the backing store for DateTime.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty DateTimeProperty =
			DependencyProperty.Register("DateTime", typeof(DateTime?), 
			typeof(NullableDateTimePicker), new UIPropertyMetadata(OnDateTimeChanged));

		#endregion

		#region OnDateTimeChanged methods

		/// <summary>
		/// Dependency Property DateTime changed listener
		/// </summary>
		/// <param name="sender">The NullableDateTimePicker whose dependency property changed</param>
		/// <param name="e">The event args that came with the change</param>
		protected static void OnDateTimeChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			(sender as NullableDateTimePicker).OnDateTimeChanged(e);
		}

		/// <summary>
		/// Listens for when the DateTime Dependency Property value changes.
		/// </summary>
		/// <param name="e">Not Used</param>
		protected void OnDateTimeChanged(DependencyPropertyChangedEventArgs e)
		{
			if (DateTime == null)
				hasValueCheckBox.IsChecked = false;
			else
				hasValueCheckBox.IsChecked = true;
		}

		#endregion

		#region hasValueCheckBox Checked and Unchecked listeners

		/// <summary>
		/// Listens for when the and shows the DateTimePicker and binds the DateTimePicker's value
		/// to DateTime
		/// </summary>
		/// <param name="sender">Not Used</param>
		/// <param name="e">Not Used</param>
		private void hasValueCheckBox_Checked(object sender, RoutedEventArgs e)
		{
			dateTimePicker.Visibility = Visibility.Visible;
			nullLabel.Visibility = Visibility.Hidden;

			if (DateTime == null)
			{
				DateTime = dateTimePicker.DateTimeSelected;
				//DateTime = SelectedDateTime;
			}

			//datePicker.CurrentlySelectedDate = (DateTime)DateTime;
			//timePicker.SelectedTime = ((DateTime)DateTime).TimeOfDay;

			//datePicker.SelectedDateChanged += SelectedDateChanged;
			//timePicker.SelectedTimeChanged += SelectedTimeChanged;

			//Bind dateTimePicker.DateTimeSelected to DateTime
			Binding bind = new Binding();
			bind.Source = this;
			bind.Path = new PropertyPath(NullableDateTimePicker.DateTimeProperty);
			bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
			bind.Mode = BindingMode.TwoWay;
			dateTimePicker.SetBinding(DateTimePicker.DateTimeSelectedProperty, bind);
		}


		/// <summary>
		/// Listens for when hasValueCheckBox is unchecked, sets nullLabel to visible,
		/// and unbinds dateTimePicker's DateTimeSelected from DateTime
		/// </summary>
		/// <param name="sender">Not Used</param>
		/// <param name="e">Not Used</param>
		private void hasValueCheckBox_Unchecked(object sender, RoutedEventArgs e)
		{
			dateTimePicker.Visibility = Visibility.Hidden;
			nullLabel.Visibility = Visibility.Visible;


			//datePicker.SelectedDateChanged -= SelectedDateChanged;
			//timePicker.SelectedTimeChanged -= SelectedTimeChanged;
			
			BindingOperations.ClearBinding(dateTimePicker, DateTimePicker.DateTimeSelectedProperty);

			if (DateTime != null)
			{
				dateTimePicker.DateTimeSelected = (DateTime)DateTime;
				//datePicker.CurrentlySelectedDate = (DateTime)DateTime;
				//timePicker.SelectedTime = ((DateTime)DateTime).TimeOfDay;
			    DateTime = null;
			}

		}

		#endregion


		///// <summary>
		///// Gets the DateTime selected by the DatePicker and TimePicker
		///// </summary>
		//private DateTime SelectedDateTime
		//{
		//    get
		//    {
		//        return new DateTime(datePicker.CurrentlySelectedDate.Year,
		//            datePicker.CurrentlySelectedDate.Month, datePicker.CurrentlySelectedDate.Day,
		//            timePicker.SelectedTime.Hours, timePicker.SelectedTime.Minutes,
		//            timePicker.SelectedTime.Seconds);
		//    }
		//}

		///// <summary>
		///// Listens for when the DatePicker is changed and 
		///// sets DateTime to the SelectedDateTime
		///// </summary>
		///// <param name="sender">Not Used</param>
		///// <param name="e">Not Used</param>
		//private void SelectedDateChanged(object sender, DateSelectedChangedRoutedEventArgs e)
		//{
		//    DateTime = SelectedDateTime;
		//}

		///// <summary>
		///// Listens for when the TimePicker is changed and
		///// sets DateTime to the SelecteDateTime
		///// </summary>
		///// <param name="sender">Not Used</param>
		///// <param name="e">Not Used</param>
		//private void SelectedTimeChanged(object sender, TimeSelectedChangedRoutedEventArgs e)
		//{
		//    DateTime = SelectedDateTime;
		//}

	}
}
